package com.ymatou.iapi.coupon.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.coupon.parameter.GetUserCouponQuantityByBatchBean;
import com.ymatou.iapi.coupon.service.GetUserCouponQuantityByBatchCall;
import com.ymatou.promotion.api.parameter.UserRetrieveSellerCoupon;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.model.coupon;
import com.ymttest.database.sqlwapper.couponWapper;
//import com.ymttest.database.model.YmtCouponPrivateUserBound;
//import com.ymttest.database.sqlwapper.YmtCouponPrivateUserBoundWapper;
import com.ymttest.utils.EnvSetup;
/**
*根据批次返回用户已领的优惠券数量
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetUserCouponQuantityByBatch{
	private static GetUserCouponQuantityByBatchBean getusercouponquantitybybatchBean;
	private static GetUserCouponQuantityByBatchCall getusercouponquantitybybatchCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据批次返回用户已领的优惠券数量");
	}

	@Before
	public void caseUp() {
		getusercouponquantitybybatchBean=new GetUserCouponQuantityByBatchBean();
		getusercouponquantitybybatchCall=new GetUserCouponQuantityByBatchCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getusercouponquantitybybatchCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
    @Category(P1.class)
    @TestCase
    public void Tc_GetUserCouponQuantityByBatch_001() {
        Logger.start(true, "返回用户在所传入的批次id中刷选已经领取过的批次id");
        try {

            setgetusercouponquantitybybatchBean();

            getusercouponquantitybybatchCall.setData(getusercouponquantitybybatchBean);
            getusercouponquantitybybatchCall.callService();

            Logger.verifyEquals("0", getusercouponquantitybybatchCall.getCode(), "验证返回statusCode");
         
            String str =",\"batchId\":"
                    + getusercouponquantitybybatchBean.getBatchIds().get(0)+
                    ",\"userId\":" + getusercouponquantitybybatchBean.getUserId() ;

            Logger.verifyEquals(true, getusercouponquantitybybatchCall.getData().contains(str), "验证返回UserId对应BatchId");

            List<UserRetrieveSellerCoupon> userRetrieveSellerCouponList =
                    getusercouponquantitybybatchCall.getUserRetrieveSellerCouponList().getUserRetrieveSellerCouponList();

            for (UserRetrieveSellerCoupon userRetrieveSellerCoupon : userRetrieveSellerCouponList) {
                Logger.verifyEquals(getusercouponquantitybybatchBean.getUserId(), userRetrieveSellerCoupon.getUserId(),
                        "验证返回UserId正确");
            }

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_GetUserCouponQuantityByBatch_002() {
        Logger.start(true, "验证UserId空时无返回");
        try {

            List<coupon> ymtCouponPrivateUserBounds =
                    new couponWapper().selectTop5();

            List<Integer> ibatchs = new ArrayList<Integer>();

            for (coupon ymtCouponPrivateUserBound : ymtCouponPrivateUserBounds) {
                ibatchs.add(ymtCouponPrivateUserBound.getBatchId());
            }
            getusercouponquantitybybatchBean.setBatchIds(ibatchs);

            getusercouponquantitybybatchCall.setData(getusercouponquantitybybatchBean);
            getusercouponquantitybybatchCall.callService();

            Logger.verifyEquals("0", getusercouponquantitybybatchCall.getCode(), "验证返回code");
            
            Logger.verifyEquals(0, getusercouponquantitybybatchCall.getUserRetrieveSellerCouponList().getUserRetrieveSellerCouponList().size(), "验证返回couponQuantity为空");
        
//            String str = "\"couponQuantitys\":[]}";
//
//            Logger.verifyEquals(true, getusercouponquantitybybatchCall.getData().contains(str), "验证返回UserId对应BatchId");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_GetUserCouponQuantityByBatch_003() {
        Logger.start(true, "验证UserId空时无返回");
        try {

        	 List<coupon> ymtCouponPrivateUserBounds =
                     new couponWapper().selectTop5();

         
            List<Integer> ibatchs = new ArrayList<Integer>();

            for (coupon ymtCouponPrivateUserBound : ymtCouponPrivateUserBounds) {
                ibatchs.add(ymtCouponPrivateUserBound.getBatchId());
            }
            
            getusercouponquantitybybatchBean.setBatchIds(ibatchs);

            getusercouponquantitybybatchCall.setData(getusercouponquantitybybatchBean);
            getusercouponquantitybybatchCall.callService();

            Logger.verifyEquals("0", getusercouponquantitybybatchCall.getCode(), "验证返回code");
            
            Logger.verifyEquals(0, getusercouponquantitybybatchCall.getUserRetrieveSellerCouponList().getUserRetrieveSellerCouponList().size(), "验证返回couponQuantity为空");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_GetUserCouponQuantityByBatch_004() {
        Logger.start(true, "验证BatchIds空时返回");
        try {

            getusercouponquantitybybatchBean.setUserId(Integer.parseInt(EnvSetup.getData("BuyUserId")));

            getusercouponquantitybybatchCall.setData(getusercouponquantitybybatchBean);
            getusercouponquantitybybatchCall.callService();

            Logger.verifyEquals("2", getusercouponquantitybybatchCall.getCode(), "验证返回code");
         
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    /**
     * 获取Ymt_CouponPrivateUserBound表中最新5条数据，用5条中的iBatchId以及第一条中的iUserId来设置请求Bean
     */
    private void setgetusercouponquantitybybatchBean()
    {
        Logger.comment("获取Ymt_CouponPrivateUserBound表中最新5条数据，用5条中的iBatchId以及第一条中的iUserId来设置请求Bean");

        List<coupon> ymtCouponPrivateUserBounds = new couponWapper().selectTop5();
        getusercouponquantitybybatchBean.setUserId(ymtCouponPrivateUserBounds.get(0).getUserId());

        List<Integer> ibatchs = new ArrayList<Integer>();

        for (coupon ymtCouponPrivateUserBound : ymtCouponPrivateUserBounds) {
            ibatchs.add(ymtCouponPrivateUserBound.getBatchId());
        }
        
        getusercouponquantitybybatchBean.setAppId("m.ymatou.com");
        getusercouponquantitybybatchBean.setRequestId("1234567890");

        getusercouponquantitybybatchBean.setBatchIds(ibatchs);
    }
}